package com.asseco.mio.codex.controllers;


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleTypes;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;





@Controller
@RequestMapping("/countries*")
public class CountryController {

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    public String test() throws SQLException {
        Configuration c = new Configuration().configure();
        SessionFactory sf = c.buildSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();

        Connection con = session.connection();  // obtain JDBC connection from Session object
        CallableStatement cs = con.prepareCall("{ call baza.prebaruvanje_klient.p_prebaruvanje_klienti_kod(?,?) }");
        cs.setString(2,"1");
        cs.registerOutParameter(1,OracleTypes.CURSOR);
                  cs.execute();
                  ResultSet resultSet = (ResultSet) cs.getObject(1);
                if (resultSet != null) {
                        while ( resultSet.next() ) {
                            System.out.println(resultSet.getMetaData().getColumnCount());
                             System.out.println(resultSet.getString(1));
                             System.out.println(resultSet.getString(2));
                        }
                        resultSet.close();
                    }

        session.getTransaction().commit();
        session.close();
        sf.close();

        return new String("countries");
    }

}
